package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.DeviceMapper;
import com.lucher.mes.dao.DevicedepotMapper;
import com.lucher.mes.po.Device;
import com.lucher.mes.po.Devicedepot;

/**
 * 用户控制层
 */
@Controller
@Scope("prototype")
@RequestMapping("/devicedepot")
@Transactional
public class DevicedepotController extends BaseController {

	@Autowired
	private DevicedepotMapper devicedepotDao;
	@Autowired
	private DeviceMapper deviceDao;
	/*
     * 特殊的instance变量
     * 注：零长度的byte数组对象创建起来将比任何对象都经济――查看编译后的字节码：
     * 生成零长度的byte[]对象只需3条操作码，而Object lock = new Object()则需要7行操作码。
     */
    private static byte[] lock = new byte[0];
	
	/**
	 * 查询材料库存
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Devicedepot form) {
		if(form.getId() != null){
			Device device = deviceDao.selectByPrimaryKey(form.getId());
			form.setDevicecode(device.getDevicecode());
			form.setDevicename(device.getDevicename());
		}
		return "devicedepot/findDevicedepotList";
	}
	
	/**
	 * 查询材料库存Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Devicedepot form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = devicedepotDao.findByCount(form);
		List<Devicedepot> list = devicedepotDao.findByListForPage(form);
		for (Devicedepot devicedepot : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			objectMap.put("devicecode", devicedepot.getDevicecode());
			objectMap.put("devicename", devicedepot.getDevicename());
			objectMap.put("batchno", devicedepot.getBatchno());
			objectMap.put("depotamount", devicedepot.getDepotamount());
			objectMap.put("addtime", StringUtils.isEmpty(devicedepot.getAddtime())?"":Tools.getStr(devicedepot.getAddtime()).substring(0, 19));
			objectMap.put("usetime", StringUtils.isEmpty(devicedepot.getUsetime())?"":Tools.getStr(devicedepot.getUsetime()).substring(0, 19));
			objectMap.put("depotstatus", devicedepot.getDepotstatus());
			objectMap.put("usearea", devicedepot.getUsearea());
			objectMap.put("info", devicedepot.getInfo());
			objectMap.put("remark", devicedepot.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("code", "0");
		result.put("count", total);//页面总数
		result.put("data", objectlist);
		return result;
	}
    
}
